package com.example.administrator.teainfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.teainfo.R;
import com.example.administrator.teainfo.adapter.FirstInPagerAdapter;
import com.example.administrator.teainfo.fragment.FirstRunFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class FirstRunActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    private ViewPager vp;
    private List<Fragment> fragments;
    private RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_run);
        init();
        vp.setAdapter(new FirstInPagerAdapter(getSupportFragmentManager(),fragments));
        vp.addOnPageChangeListener(this);
    }

    private void init() {
        vp = (ViewPager) findViewById(R.id.activity_first_run_vp);
        rg = (RadioGroup) findViewById(R.id.activity_first_run_rg);
        fragments = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            FirstRunFragment frf = new FirstRunFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type",i);
            frf.setArguments(bundle);
            fragments.add(frf);
        }
        setRB();
    }

    private void setRB() {
        for (int i = 0; i < fragments.size(); i++) {
            RadioButton rb = new RadioButton(this);
            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                    ,ViewGroup.LayoutParams.MATCH_PARENT,1);
            lp.setMargins(5,5,5,5);
            rb.setLayoutParams(lp);
            rb.setButtonDrawable(android.R.color.transparent);
            rb.setBackgroundResource(R.drawable.rg_rb);
            rb.setEnabled(false);
            rg.addView(rb);
            Log.e("dd", "setRB: "+rg.getChildCount());
        }
        rg.getChildAt(0).setEnabled(true);

    }

    //监听viewPager
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setRG(position);//判断rg的选中状态
        if (position==fragments.size()-1) {
            final Intent intent = new Intent(this,MainActivity.class);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    startActivity(intent);
                    finish();
                }
            },1500);
        }
    }

    private void setRG(int position) {
        for (int i = 0; i < fragments.size(); i++) {
            if (i==position) {
                rg.getChildAt(position).setEnabled(true);
            }else {
                rg.getChildAt(i).setEnabled(false);
            }
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
